package com.xxx.service.impl;

import com.xxx.dao.ReservationDao;
import com.xxx.entity.MPage;
import com.xxx.entity.Reservation;
import com.xxx.dao.ReservationDao;
import com.xxx.entity.dto.ReservationDto;
import com.xxx.entity.query.ReservationQuery;
import com.xxx.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xxx
 * @since 2024-11-24
 */
@Service
@Transactional
public class ReservationServiceImpl extends ServiceImpl<ReservationDao, Reservation> implements IReservationService {


    @Autowired
    private ReservationDao reservationDao;

    @Override
    public MPage selectByCondition(ReservationQuery reservationQuery) {
        int current = reservationQuery.getCurrent();
        int size = reservationQuery.getSize();
        reservationQuery.setCurrent(null);
        List<ReservationDto> list = reservationDao.selectByCondition(reservationQuery);
        int total = list.size();
        reservationQuery.setCurrent((current-1)*size);
        list = reservationDao.selectByCondition(reservationQuery);
        MPage page = MPage.builder()
                .current(current)
                .size(size)
                .total(total)
                .pages(total/size+total%size>0?1:0)
                .records(list)
                .build();
        return page;
    }

    @Override
    public MPage selectByPage(Integer current, Integer size) {
        int total = reservationDao.selectCount(null);
        List<ReservationDto> list = reservationDao.selectByPage((current-1)*size,size);
        MPage page = MPage.builder()
                .current(current)
                .size(size)
                .total(total)
                .pages(total/size+total%size>0?1:0)
                .records(list)
                .build();
        return page;
    }
}
